package com.qf.search.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/search")
@RefreshScope
public class SearchController {

    @Value("${server.port}")
    private Integer port;

    @Value("${version}")
    private String version;   //v1.0

    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/hello")
    public String hello(){
        return "SEARCH：Hello!!" + port + ":" + version;
    }


    @GetMapping("/find/{id}")
    public String findById(@PathVariable String id){
        if("1".equalsIgnoreCase(id)){
            throw new RuntimeException("凉凉~~~");
        }
        System.out.println("接收到参数id:" + id);
        return "接收到参数id:" + id;
    }

    @GetMapping("/find")
    public String findByCondition(@RequestParam String name, String age){
        System.out.println("接收到参数name" + name + ",age:" + age);
        return "接收到参数name" + name + ",age:" + age;
    }

    @PostMapping("/save")
    public String save(@RequestBody Map map) throws JsonProcessingException {
        System.out.println("接收到参数map:" + map);
        return "接收到参数map:" + objectMapper.writeValueAsString(map);
    }




}
